package cz.zcu.fav.rat.managed.bean.calendar;

import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

import cz.zcu.fav.rat.beans.Object;
import cz.zcu.fav.rat.beans.Period;

import org.primefaces.model.ScheduleModel;

import cz.zcu.fav.rat.beans.User;
import cz.zcu.fav.rat.managed.bean.calendar.event.ReservationEventFactory;

@ManagedBean(name = "objectCalendarBean")
@ViewScoped
public class ObjectCalendarBean extends AbstractCalendarBean<Object>
{
	private static final long serialVersionUID = -556025997527923647L;
	protected List<User> users;

	// @Override
	// @PostConstruct
	// public void init()
	// {
	// super.init();
	// }

	@Override
	protected ScheduleModel getModel(Integer identifier)
	{
		return new CalendarModelEventObject(identifier,
				new ReservationEventFactory());
	}

	public List<User> getUsers()
	{
		if (users == null)
		{
			users = getUserService().getAll();
		}
		return users;
	}

	@Override
	protected Object loadItem(Integer itemId)
	{
		return getObjectservice().get(itemId);
	}

	@Override
	protected ReservationEvent createNewEvent(Date since, Date to)
	{
		return new ReservationEvent("dummy", since, to, null, getItem(), null,
				null);
	}

	public void pickUpEvent(ActionEvent actionEvent)
	{
		Period period = getPeriodService().pickUp(
				getEvent().getObject().getId(), getEvent().getUser());
		if (period != null)
		{
			if (!period.getReservation().getUser().equals(getEvent().getUser()))
			{
				// this is not my reservation
				// TODO: notify somehow
			} else
			{
				// /fine
				// TODO: notify somehow
			}
		} else
		{
			System.out.print("sdfsdf");
			// TODO: notify somehow

			// nic nebylo picknutý

		}
	}
}